--奖斥有度
local jiangchiyoudu = fk.CreateSkill {
    name = "mcdh_jiangchiyoudu"
}
local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

jiangchiyoudu:addEffect(fk.EventPhaseStart,{
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self) and not player:isKongcheng() and target.phase == Player.Start
    end,
    on_cost = function (self, event, target, player, data)
        if  target.phase == Player.Start then
            local room = player.room
            local cards = room:askForDiscard(player, 1, 999, true, self.name, true, ".","#mcdh_jiangchiyoudu_askfor", true)
            if #cards > 0 then
                self.cost_data = cards
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        room:throwCard(self.cost_data, self.name, player, player)
        local mark = target:getTableMark("#mcdh_jiangchiyoudu-turn")
        local n = 0
        local use_events = player.room.logicgetActualDamageEvents(999, function(e)
            local damage = e.data
            if damage.from == target then
                n = n + 1
            end
        end, Player.HistoryTurn)
        table.insert(mark,{n,#self.cost_data,player})
        room:setPlayerMark(target,"#mcdh_jiangchiyoudu-turn",mark)
        room:setPlayerMark(player,"@mcdh_jiangchiyoudu-turn","<font color='red'>"..tostring(n).."</font>".." "..tostring(#self.cost_data))
    end,
})

jiangchiyoudu:addEffect(fk.Damage,{
    can_refresh = function (self, event, target, player, data)
      return player == target and target:getTableMark("#mcdh_jiangchiyoudu-turn") ~= {}
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local mark = player:getMark("#mcdh_jiangchiyoudu-turn")
        for i, v in ipairs(mark) do
            v[1] = v[1] + 1
            if v[1] == v[2] then
                data.extra_data = data.extra_data or {}
                data.extra_data.jiangchiyoudu = data.extra_data.jiangchiyoudu or 0
                data.extra_data.jiangchiyoudu = data.extra_data.jiangchiyoudu + 1
            end
            if v[1] < v[2] then
                room:setPlayerMark(v[3],"@mcdh_jiangchiyoudu-turn","<font color='red'>"..tostring(mark[1]).."</font>".." "..tostring(mark[2]))
            else
                room:setPlayerMark(v[3],"@mcdh_jiangchiyoudu-turn","<font color='green'>"..tostring(mark[1]).."</font>".." "..tostring(mark[2]))
            end
        end
    end
})

jiangchiyoudu:addEffect(fk.Damage,{
    triggerableTimes = function(self, event, target, player, data)
        if data.extra_data and data.extra_data.jiangchiyoudu  then
            return data.extra_data.jiangchiyoudu
        else
            return 1
        end
    end,
    can_trigger = function(self, event, target, player, data)
           return target == player and data.extra_data and data.extra_data.jiangchiyoudu
    end,
    on_cost = function (self, event, target, player, data)
        return true
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        target:drawCards(2,self.name)
    end,
})

jiangchiyoudu:addEffect(fk.EventPhaseStart,{
    can_trigger = function(self, event, target, player, data)
        local room = player.room
        if target.phase == Player.Finish and target == player then
            local mark = player:getMark("#mcdh_jiangchiyoudu-turn")
            local players = {}
            for i, v in ipairs(mark) do
                v[1] = v[1] + 1
                if v[1] < v[2] then
                    table.insert(players,v[3])
                end
            end
            room:sortByAction(players)
            self.cost_data = players
            return #players ~= 0
        end
    end,
    on_cost = function (self, event, target, player, data)
            return true
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        for i, v in ipairs(self.cost_data) do
            room:damage{
                from = player,
                to = target,
                damage = 1,
                skillName = self.name,
            }
        end
    end,
})

Fk:loadTranslationTable{
    ["mcdh_jiangchiyoudu"] = "奖斥",
    [":mcdh_jiangchiyoudu"] = "『奖斥有度』<br>一名角色的准备阶段，你可以弃置至少一张牌。若如此做：其本回合造成第X次伤害后，你令其摸两张牌；本回合结束阶段，若其造成伤害次数小于X，你对其造成1点伤害。（X为你弃置的牌数）",
    ["@mcdh_jiangchiyoudu-turn"] = "奖斥有度",
    ["#mcdh_jiangchiyoudu_askfor"] = "奖斥有度：你可以弃置至少一张牌。若如此做：当前回合角色本回合造成第X次伤害后，其摸两张牌；<br>本回合结束阶段，若其造成伤害次数小于X，你对其造成1点伤害。（X为你弃置的牌数）",
}
return jiangchiyoudu